package com.alks.function.service.impl;

import com.alks.entity.data.entity.SdCompany;
import com.alks.entity.data.enums.ResultCodeEnum;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.mapper.SdCompanyMapper;
import com.alks.function.service.SdCompanyService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Slf4j
@Service
public class SdCompanyServiceImpl extends ServiceImpl<SdCompanyMapper, SdCompany> implements SdCompanyService {


    @Autowired
    private SdCompanyMapper sdCompanyMapper;

    /***
     * 根据id查询
     * @param companyId
     * @return
     */
    @Override
    public ResponseInfo FindCompanyInfoById(String companyId) {
        if (StringUtils.isEmpty(companyId)) {
            return ResponseInfo.error(ResultCodeEnum.LOGIN_CONFIG_IS_NULL.getMessage(), ResultCodeEnum.LOGIN_CONFIG_IS_NULL.getCode());
        }
        LambdaQueryWrapper<SdCompany> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SdCompany::getCompanyId, companyId);
        SdCompany one = getOne(queryWrapper);

        return ResponseInfo.ok(one);
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public ResponseInfo FindAll() {
        return ResponseInfo.ok(sdCompanyMapper.FindAll());
    }

    /**
     * 添加公司信息
     * @param sdCompany
     * @return
     */
    @Override
    public ResponseInfo SaveCompanyInfo(SdCompany sdCompany) {
        String companyId = sdCompany.getCompanyId();
        if (companyId != null) {
            //把标点符号清洗
            String symbolStr = companyId.replaceAll("\\p{P}", "");
            if (symbolStr.length() != companyId.length()) {
                return ResponseInfo.error(ResultCodeEnum.COMPANY_NAME_NOT_SYMBOL.getMessage(), ResultCodeEnum.COMPANY_NAME_NOT_SYMBOL.getCode());
            }

            //正则表达式 判断是否中文
            Pattern ChineseStr = Pattern.compile("[\u4E00-\u9FA5]");
            Matcher Chinese = ChineseStr.matcher(companyId);
            if (Chinese.find() == true) {
                return ResponseInfo.error(ResultCodeEnum.COMPANY_NAME_NOT_ENGLISH.getMessage(), ResultCodeEnum.COMPANY_NAME_NOT_ENGLISH.getCode());
            }

            for (int i = 0; i < companyId.length(); i++) {
                //判断是否大写
                if (!Character.isUpperCase(companyId.charAt(i))) {
                    return ResponseInfo.error(ResultCodeEnum.COMPANY_NAME_NOT_CAPITAL.getMessage(), ResultCodeEnum.COMPANY_NAME_NOT_CAPITAL.getCode());
                }
            }
        }

        QueryWrapper<SdCompany> wrapper = new QueryWrapper<>();
        wrapper.eq(companyId != null, "COMPANY_ID", companyId);
        SdCompany one = this.getOne(wrapper);
        if (one != null) {
            return ResponseInfo.error(ResultCodeEnum.COMPANY_IS_EXIST.getMessage(), ResultCodeEnum.COMPANY_IS_EXIST.getCode());
        }
        boolean save = this.save(sdCompany);
        if (save == false) {
            return ResponseInfo.error(ResultCodeEnum.ADD_ERROR.getMessage(), ResultCodeEnum.ADD_ERROR.getCode());
        }

        return ResponseInfo.ok(ResultCodeEnum.ADD_SUCCESS);
    }

    /**
     * 修改
     * @param sdCompany
     */
    @Override
    public ResponseInfo UpdateWithCompanyInfo(SdCompany sdCompany) {
        QueryWrapper<SdCompany> wrapper = new QueryWrapper<>();
        wrapper.eq(sdCompany.getCompanyId() != null, "COMPANY_ID", sdCompany.getCompanyId());
        SdCompany one = this.getOne(wrapper);
        if (one == null) {
            return ResponseInfo.error(ResultCodeEnum.LOGIN_CONFIG_IS_NULL.getMessage(), ResultCodeEnum.LOGIN_PASSWORD_IS_NULL.getCode());
        }
        sdCompanyMapper.UpdateWithCompanyInfo(sdCompany);
        return ResponseInfo.ok(ResultCodeEnum.UPDATE_ERROR);
    }

}
